﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormPaymentList : FormTemplet
    {
        private OrderCondition _oc = null;

        private DataTable _dtPayments = null;

        public FormPaymentList()
        {
            this.InitializeComponent();
        }

        public FormPaymentList(OrderCondition oc) : this()
        {
            this._oc = oc;
            this.LoadPaymentsByCondition();
        }

        private void LoadPaymentsByCondition()
        {
            Orders orders = new Orders();
            try
            {
                this._dtPayments = orders.GetPaymentsByCondition(this._oc);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n连接数据库的通道出现故障，请检查！\r\n\r\n信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            if (this._dtPayments != null)
            {
                if (this._dtPayments.Rows.Count < 1)
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgPayment.DataSource = null;
                    base.Dispose();
                }
                else
                {
                    this.dgPayment.DataSource = null;
                    this.dgPayment.DataSource = this._dtPayments;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void FormPaymentInfo_Load(object sender, EventArgs e)
        {
            this.dgPayment.AutoResizeColumns();
        }

        private void btnShowTotal_Click(object sender, EventArgs e)
        {
            if (this._dtPayments != null)
            {
                if (this._dtPayments.Rows.Count < 1)
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    double num = 0.0;
                    for (int i = 0; i < this._dtPayments.Rows.Count; i++)
                    {
                        num += Convert.ToDouble(this._dtPayments.Rows[i]["付款金额"]);
                    }
                    this.txtResult.Text = "款项：" + num.ToString();
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dgPayment.Rows.Count < 1)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                int rowIndex = this.dgPayment.CurrentCell.RowIndex;
                FormUpdateOrderPayment formUpdateOrderPayment = new FormUpdateOrderPayment((long)Convert.ToInt32(this.dgPayment.Rows[rowIndex].Cells[0].Value));
                formUpdateOrderPayment.MdiParent = base.MdiParent;
                formUpdateOrderPayment.Show();
                formUpdateOrderPayment.Activate();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dgPayment.Rows.Count < 1)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Orders orders = new Orders();
                bool flag = false;
                int rowIndex = this.dgPayment.CurrentCell.RowIndex;
                try
                {
                    flag = orders.DeletePayment((long)Convert.ToInt32(this.dgPayment.Rows[rowIndex].Cells[0].Value), this.dgPayment.Rows[rowIndex].Cells[1].Value.ToString(), LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    if (ex.Number < 50000)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (flag)
                {
                    MessageBox.Show("操作成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.dgPayment.Rows.Remove(this.dgPayment.CurrentRow);
                }
            }
        }
    }
}
